{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "numpy.ndarray size changed, may indicate binary incompatibility. Expected 88 from C header, got 80 from PyObject",
     "output_type": "error",
     "traceback": [
      "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[0;31mValueError\u001B[0m                                Traceback (most recent call last)",
      "\u001B[0;32m/tmp/ipykernel_16512/723076895.py\u001B[0m in \u001B[0;36m<module>\u001B[0;34m\u001B[0m\n\u001B[0;32m----> 1\u001B[0;31m \u001B[0;32mimport\u001B[0m \u001B[0mpandas\u001B[0m \u001B[0;32mas\u001B[0m \u001B[0mpd\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0m\u001B[1;32m      2\u001B[0m \u001B[0;32mfrom\u001B[0m \u001B[0mptrail\u001B[0m\u001B[0;34m.\u001B[0m\u001B[0mcore\u001B[0m\u001B[0;34m.\u001B[0m\u001B[0mTrajectoryDF\u001B[0m \u001B[0;32mimport\u001B[0m \u001B[0mPTRAILDataFrame\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[1;32m      3\u001B[0m \u001B[0;34m\u001B[0m\u001B[0m\n\u001B[1;32m      4\u001B[0m \u001B[0;31m# The following link contains the traffic dataset\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[1;32m      5\u001B[0m \u001B[0murl_traffic\u001B[0m \u001B[0;34m=\u001B[0m \u001B[0;34m'https://raw.githubusercontent.com/YakshHaranwala/PTRAIL/main/examples/data/traffic.csv'\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n",
      "\u001B[0;32m~/anaconda3/envs/PreprocessingLibrary/lib/python3.8/site-packages/pandas/__init__.py\u001B[0m in \u001B[0;36m<module>\u001B[0;34m\u001B[0m\n\u001B[1;32m     27\u001B[0m \u001B[0;34m\u001B[0m\u001B[0m\n\u001B[1;32m     28\u001B[0m \u001B[0;32mtry\u001B[0m\u001B[0;34m:\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0;32m---> 29\u001B[0;31m     \u001B[0;32mfrom\u001B[0m \u001B[0mpandas\u001B[0m\u001B[0;34m.\u001B[0m\u001B[0m_libs\u001B[0m \u001B[0;32mimport\u001B[0m \u001B[0mhashtable\u001B[0m \u001B[0;32mas\u001B[0m \u001B[0m_hashtable\u001B[0m\u001B[0;34m,\u001B[0m \u001B[0mlib\u001B[0m \u001B[0;32mas\u001B[0m \u001B[0m_lib\u001B[0m\u001B[0;34m,\u001B[0m \u001B[0mtslib\u001B[0m \u001B[0;32mas\u001B[0m \u001B[0m_tslib\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0m\u001B[1;32m     30\u001B[0m \u001B[0;32mexcept\u001B[0m \u001B[0mImportError\u001B[0m \u001B[0;32mas\u001B[0m \u001B[0me\u001B[0m\u001B[0;34m:\u001B[0m  \u001B[0;31m# pragma: no cover\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[1;32m     31\u001B[0m     \u001B[0;31m# hack but overkill to use re\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n",
      "\u001B[0;32m~/anaconda3/envs/PreprocessingLibrary/lib/python3.8/site-packages/pandas/_libs/__init__.py\u001B[0m in \u001B[0;36m<module>\u001B[0;34m\u001B[0m\n\u001B[1;32m     11\u001B[0m \u001B[0;34m\u001B[0m\u001B[0m\n\u001B[1;32m     12\u001B[0m \u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0;32m---> 13\u001B[0;31m \u001B[0;32mfrom\u001B[0m \u001B[0mpandas\u001B[0m\u001B[0;34m.\u001B[0m\u001B[0m_libs\u001B[0m\u001B[0;34m.\u001B[0m\u001B[0minterval\u001B[0m \u001B[0;32mimport\u001B[0m \u001B[0mInterval\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0m\u001B[1;32m     14\u001B[0m from pandas._libs.tslibs import (\n\u001B[1;32m     15\u001B[0m     \u001B[0mNaT\u001B[0m\u001B[0;34m,\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n",
      "\u001B[0;32mpandas/_libs/interval.pyx\u001B[0m in \u001B[0;36minit pandas._libs.interval\u001B[0;34m()\u001B[0m\n",
      "\u001B[0;31mValueError\u001B[0m: numpy.ndarray size changed, may indicate binary incompatibility. Expected 88 from C header, got 80 from PyObject"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from ptrail.core.TrajectoryDF import PTRAILDataFrame\n",
    "\n",
    "# The following link contains the traffic dataset\n",
    "url_traffic = 'https://raw.githubusercontent.com/YakshHaranwala/PTRAIL/main/examples/data/traffic.csv'\n",
    "data_traffic = pd.read_csv(url_traffic)\n",
    "\n",
    "traffic_df = PTRAILDataFrame(data_set=data_traffic,\n",
    "                        latitude='lat',\n",
    "                        longitude='lon',\n",
    "                        datetime='datetime',\n",
    "                        traj_id='id')\n",
    "print(traffic_df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "from ptrail.features.kinematic_features import KinematicFeatures as spatial\n",
    "\n",
    "traffic_df = spatial.create_acceleration_column(traffic_df)\n",
    "traffic_df = spatial.create_bearing_column(traffic_df)\n",
    "traffic_df = spatial.create_jerk_column(traffic_df)\n",
    "trajectories = traffic_df.index.unique(level=\"traj_id\")\n",
    "\n",
    "# Creating a dataset with mean of values\n",
    "traj_df_list = []\n",
    "for traj in trajectories:\n",
    "    traj_df = pd.DataFrame(traffic_df.loc[[traj]].mean()).transpose()\n",
    "    traj_df['traj_id'] = traj\n",
    "    traj_df_list.append(traj_df)\n",
    "\n",
    "mean_df = pd.concat(traj_df_list, ignore_index=True)\n",
    "drop_list = ['vehicle_type', 'lon', 'lat', 'kilopost',\n",
    "             'detected_flag', 'traj_id']\n",
    "mean_df = mean_df.drop(drop_list, axis=1)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# Importing pycaret and creating a model to\n",
    "# estimate the length of the vehicle\n",
    "from pycaret.regression import *\n",
    "\n",
    "length_reg = setup(data = mean_df,\n",
    "                   target = 'vehicle_length',\n",
    "                   normalize=True)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# Lets try to find the best model\n",
    "compare_models()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# Huber was the best one during the run so\n",
    "# we'll create a new and tune it\n",
    "huber = create_model('huber')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "tuned_huber = tune_model(huber)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# We can perform some plots to undestand it better\n",
    "plot_model(tuned_huber)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "plot_model(tuned_huber, plot = 'error')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "plot_model(tuned_huber, plot='feature')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}